package com.crm.system.service.impl.returnMoney;

import com.crm.common.model.DhPageInfo;
import com.crm.model.qo.returnMoney.ModifyRoyaltyStatusQO;
import com.crm.model.vo.returnMoney.ModifyRoyaltyStatusVO;
import com.crm.service.returnMoney.ModifyRoyaltyStatusDbService;
import com.crm.system.service.returnMoney.ModifyRoyaltyStatusService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/11/26 11:04
 */
@Service
public class ModifyRoyaltyStatusServiceImpl implements ModifyRoyaltyStatusService {
    @Autowired
    private ModifyRoyaltyStatusDbService modifyRoyaltyStatusDbService;

    /**
     * 财务 - 回款管理 - 到款记录 - “修改回款提成发放状态”列表
     * @Author pym
     * @Date  2021/11/26 14:48
     * @param pageIndex -页码
     * @param pageSize -分页大小
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @param modifyRoyaltyStatusQO -筛选条件
     * @return {@link {com.crm.common.model.DhPageInfo<com.crm.model.vo.returnMoney.ModifyRoyaltyStatusVO>}}
     */
    @Override
    public DhPageInfo<ModifyRoyaltyStatusVO> selectModifyRoyaltyStatusList(Integer pageIndex, Integer pageSize,
                                                                           String sortName, String sortOrder,
                                                                           ModifyRoyaltyStatusQO modifyRoyaltyStatusQO) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        //排序字符串拼接
        String order = getOrder(sortName, sortOrder);
        List<ModifyRoyaltyStatusVO> modifyRoyaltyStatusVOList = modifyRoyaltyStatusDbService.selectModifyRoyaltyStatusList(order, modifyRoyaltyStatusQO);
        PageInfo<ModifyRoyaltyStatusVO> pageInfo = new PageInfo<ModifyRoyaltyStatusVO>(modifyRoyaltyStatusVOList);
        return new DhPageInfo<ModifyRoyaltyStatusVO>(pageInfo,modifyRoyaltyStatusVOList);
    }

    /**
     * 排序字符串拼接
     * @Author pym
     * @Date  2021/8/19 10:45
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @return {@link {java.lang.String}}
     */
    private String getOrder(String sortName, String sortOrder) {
        String order = "";
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch(sortName){
                case "operateTime":
                { //操作时间
                    order = getOrder(order, sortOrder, "operate_time");
                }
                break;
                case "operateQuantity":
                { //操作数量
                    order = getOrder(order, sortOrder, "operate_quantity");
                }
                break;
                case "operatePeopleName":
                { //操作人Id
                    order = getOrder(order, sortOrder, "operate_people_name");
                }
                break;
                default:
                    break;
            }
        }
        if (!order.contains("operate_time")) {
            order = getOrder(order, "DESC", "operate_time");
        }
        return order;
    }
    /**
     * 排序字符串拼接
     * @Author pym
     * @Date  2021/8/19 10:45
     * @param order -排序字符串
     * @param sortName -排序列名字
     * @param sortOrder -排序规则(asc/desc)
     * @return {@link {java.lang.String}}
     */
    private String getOrder(String order, String sortOrder, String sortName) {
        if (StringUtils.isBlank(order)) {
            order += sortName +" "+ sortOrder;
        } else {
            order += ","+sortName +" " + sortOrder;
        }
        return order;
    }
}
